package com.tiger.admin.http.banner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiger.admin.Db;
import com.tiger.admin.SaveOperateLog;
import com.tiger.admin.dto.PaginationResult;
import com.tiger.admin.http.banner.dto.PageDto;
import com.tiger.common.UploadService;
import com.tiger.db.entity.Banner;
import com.tiger.utils.ResponseUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.Instant;
import java.util.*;

@RestController
@RequestMapping("/banner")
public class BannerController {
    private final Db db;

    private final UploadService uploadService;

    public BannerController(Db db, UploadService uploadService) {
        this.db = db;
        this.uploadService = uploadService;
    }

    @PostMapping("upload")
    public Object uploadPic(@RequestParam("pic") MultipartFile file, @RequestAttribute("memberId") long memberId) {
        String fileName = uploadService.put(file, String.valueOf(memberId));
        return ResponseUtil.successRes(fileName);
    }

    @PostMapping("page")
    public Object page(@RequestBody PageDto page, @RequestAttribute("memberId") long memberId) {
        PaginationResult<Banner> result = new PaginationResult<>();

        LambdaQueryWrapper<Banner> wrapper = new LambdaQueryWrapper<>();

        if(page.getName() != null && !page.getName().isEmpty()) {
            wrapper.like(Banner::getName, page.getName());
        }


        long total = db.banner.count(wrapper);
        if(total > 0) {
            result.setTotal(total);

            wrapper.orderByDesc(Banner::getSort);
            wrapper.orderByDesc(Banner::getId);

            List<Banner> rows = db.banner.pagination(wrapper, page.getPageNo(), page.getPageSize());



            result.setRows(rows);
        }


        return ResponseUtil.successRes(result);
    }

    @SaveOperateLog("新增banner")
    @PostMapping("add")
    public Object add(@RequestBody @Validated Banner data, @RequestAttribute("memberId") long memberId) {

        Instant now = Instant.now();

        data.setCreateTime(now.getEpochSecond());

        data.setCreatorId(memberId);

        boolean r = db.banner.save(data);

        return r ? ResponseUtil.successRes(data) : ResponseUtil.failRes();

    }
    @SaveOperateLog("修改banner")
    @PostMapping("update/{id}")
    public Object add(@RequestBody @Validated Banner data, @PathVariable Integer id, @RequestAttribute("memberId") long memberId) {
        Banner row = db.banner.getById(id);
        if(row == null) return ResponseUtil.successRes();

        String oldCover = null;

        if(data.getCover() != null && !data.getCover().equals(row.getCover())) {
            oldCover = row.getCover();
        }

        Instant now = Instant.now();

        BeanUtils.copyProperties(data, row);


        row.setId(id);
        row.setUpdateTime(now.getEpochSecond());


        boolean r = db.banner.updateById(row);

        if(r) {
            if(oldCover != null) {
                uploadService.del(oldCover);
            }
            return ResponseUtil.successRes(row);
        }



        return ResponseUtil.failRes();

    }

    @SaveOperateLog("删除banner")
    @PostMapping("delete/{id}")
    public Object del(@PathVariable long id, @RequestAttribute("memberId") long memberId) {
        Banner row = db.banner.getOne(new QueryWrapper<Banner>().select("id", "cover").eq("id", id));
        if(row == null) return ResponseUtil.successRes();
        boolean r = db.banner.removeById(id);
        if(r) {
            uploadService.del(row.getCover());
            return ResponseUtil.successRes();
        }
        return ResponseUtil.failRes();
    }
}
